<?php

/**
 * 前台 首页 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: index.class.php 39 2011-07-11 17:12:35Z zwt007 $
 * http://www.t-mac.org；
 */
class index extends Model
{

    private $article_table;
    private $link_table;
    private $post_table;   

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->article_table = '#@__article';
        $this->link_table = '#@__links';
        $this->post_table = '#@__post';
    }

    /**
     * 取单页面的内容
     * @param <type> $name
     * @return <type>
     */
    public function getPage($name)
    {
        $sql = "SELECT title, content FROM $this->article_table WHERE name = '$name' AND type = 'page' LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        return $rs;
    }

    public function getCommon()
    {
        $uid = empty($_SESSION['uid']) ? 0 : $_SESSION['uid'];
        $username = empty($_SESSION['username']) ? '' : $_SESSION['username'];
        //取config配置文件
        $configcache = Tmac::config('configcache.config', 'admin', '.inc.php');
        $configcache['cfg_powerby'] = $this->HD($configcache['cfg_powerby']);
        //取左边的最新博客
        $this->getPostListLimit('edit_time DESC, id DESC');

        $this->assign('config', $configcache);
        $this->assign('uid', $uid);
        $this->assign('username', $username);
    }

    /**
     * 取内容
     * @param <type> $limit
     * @return <type>
     */
    public function getArticleLimit($type, $limit=12, $cat_id=1)
    {
        //取左边的产品列表
        if ($type == 'product') {
            $sql = "SELECT article_id, title, attachments, time FROM $this->article_table WHERE type='$type' ORDER BY article_id DESC LIMIT 0,{$limit}";
        } elseif ($type == 'post') {
            $sql = "SELECT article_id, title, attachments, time FROM $this->article_table WHERE type='$type' AND cat_id='$cat_id' ORDER BY article_id DESC LIMIT 0,{$limit}";
        }
        $rs = $this->db->getAll($sql);
        return $rs;
    }


    /**
     * 递归取顶级栏目的别名
     * @param <type> $value
     * @param <type> $type
     * @return <type>
     */
    public function getToptypeNmae($value, $type = 'name')
    {
        if ($type == 'name') {
            $catinfo = $this->M('show')->getCategoryInfo($value);
        } else {
            $catinfo = $this->M('show')->getCategoryInfoById($value);
        }

        if ($catinfo['cat_pid'] <> 0) {
            $this->getToptypeNmae($catinfo['cat_id'], 'cat_id');
        }
        return $catinfo['category_nicename'];
    }

    /**
     * 取博客
     * @param <type> $limit
     * @return <type>
     */
    public function getPostListLimit($orderby = 'id DESC', $limit=6)
    {        
        $sql = "SELECT * FROM $this->post_table ORDER BY $orderby LIMIT 0, $limit";
        $rs = $this->db->getAll($sql);
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['post_time'] = date('Y-m-d', $v['post_time']);
                $rs[$k]['edit_time'] = date('Y-m-d', $v['edit_time']);
            }
        }

        $count = count($rs);
        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无新博客! ";
        }

        $this->assign('left_blog_rs', $rs);
        $this->assign('left_blog_ErrorMsg', $ErrorMsg);
        return $rs;
    }

    /**
     * 取友情链接
     * @param <type> $type
     * @param <type> $name
     * @param <type> $limit
     * @return <type>
     */
    public function getlink($type = 'img', $name = 'link_img_array', $limit = 8)
    {
        $time = time();
        $sql = "SELECT * FROM $this->link_table ";
        $sql .= "WHERE IF(link_state_radio = 1, link_starttime<$time AND link_endtime>$time, link_state_radio <> 3) ";
        if ($type == 'img') {
            $sql .= " AND link_type_radio <> 1 ";
        } else {
            $sql .= " AND link_type_radio = 1 ";
        }

        $sql .= "ORDER BY link_order DESC ";
        if ($limit > 0)
            $sql .= "LIMIT 0, $limit";
        $rs = $this->db->cacheGetAll($sql, 3600);
        if ($type == 'img') {   //如果是图片类型的友链
            foreach ($rs AS $k => $v) {
                if ($v['link_type_radio'] == 3) {
                    $rs[$k]['img'] = $v['link_image'];
                } else {
                    $rs[$k]['img'] = $v['link_image'];
                }
            }
        }
        $this->assign($name, $rs);
        return $rs;
    }
}